package pojo.other;

import Mysqlcon.Conn;
import uilt.Scan;

import java.sql.ResultSet;

public class Knowledge extends Conn {
    Scan sc = new Scan();

    public void menu(){
        while (true) {
            System.out.println("******欢迎来到创建知识点******");
            System.out.println("1.添加知识点");
            System.out.println("2.知识点列表");
            System.out.println("3.删除知识点");
            System.out.println("4.退       出");
            System.out.print("请输入：");
            int i = sc.getkey();

            if (i == 1) {
                System.out.println("******欢迎来到创建知识点******");
                System.out.print("请输入知识点内容:");
                String content = sc.getString();
                insertKnowledge(content);
            } else if (i == 2) {
                System.out.println("******欢迎来到知识点列表展示******");
                showKnowledge();
            }
            else if(i == 3){
                System.out.println("******欢迎来到删除标签******");
                System.out.print("请输入想要删除的知识点:");
                String knowledge = sc.getString();
                deleteKnowledge(knowledge);
            }
            else {
                break;
            }
        }

    }
    //插入知识点
    public void insertKnowledge(String content){
        try {
            String sql = "insert into knowledge values(?,NOW())";
            prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setString(1,content);
            int num = prepareStatement.executeUpdate();
            if (num > 0) {
                System.out.println("恭喜，知识点添加成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //展示知识点
    public void showKnowledge(){
        try{
            sta = connection.createStatement();

            String sql = "select knowledgename,createtime from knowledge";

            ResultSet rs = sta.executeQuery(sql);

            while(rs.next()){
                System.out.println("知识点名称:"+rs.getString("knowledgename")+";创建时间:"+rs.getString("createtime"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //删除知识点
    public void deleteKnowledge(String knowledge){
        try {

            String sql = "delete from knowledge where knowledgename = ?";
            prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setString(1,knowledge);
            int num = prepareStatement.executeUpdate();
            if (num > 0) {
                System.out.println("恭喜，删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
